﻿using Microsoft.IdentityModel.Tokens;
using SanJ.FreeCare.ValueSets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;

namespace SanJ.FreeCare.Schedules
{
    public class ScheduleManager : DomainService
    {
        private readonly IScheduleRepository _scheduleRepository;
        private readonly IScheduleWorkTimeRepository _scheduleWorkTimeRepository;
        private readonly IScheduleHolidayRepository _scheduleHolidayRepository;
        

        public ScheduleManager(IScheduleRepository scheduleRepository, IScheduleWorkTimeRepository scheduleWorkTimeRepository, IScheduleHolidayRepository scheduleHolidayRepository)
        {
            _scheduleRepository = scheduleRepository;
            _scheduleWorkTimeRepository = scheduleWorkTimeRepository;
            _scheduleHolidayRepository = scheduleHolidayRepository;

        }

        public async Task<ScheduleWorkTime> CreateWorkTimeAsync(ScheduleWorkTime scheduleWorkTime)
        {
            if (string.IsNullOrEmpty(scheduleWorkTime.IsVaild))
            {
                scheduleWorkTime.IsVaild = VaildType.Vaild;
            }

            return await _scheduleWorkTimeRepository.InsertAsync(scheduleWorkTime);
        }

        public async Task<ScheduleWorkTime> UpdateWorkTimeAsync(ScheduleWorkTime scheduleWorkTime)
        {
            return await _scheduleWorkTimeRepository.UpdateAsync(scheduleWorkTime);
        }

        public Task<List<ScheduleWorkTime>> GetWorkTimeAsync()
        {
            return _scheduleWorkTimeRepository.GetListAsync();
        }

        public async Task<ScheduleWorkTime> DeleteWorkTimeAsync(ScheduleWorkTime scheduleWorkTime)
        {
            try
            {
                await _scheduleWorkTimeRepository.DeleteAsync(scheduleWorkTime);
                scheduleWorkTime.IsDeleted = true;
                return scheduleWorkTime;
            }
            catch (Exception e)
            {
                scheduleWorkTime.IsDeleted = false;
                return scheduleWorkTime;
            }
        }

        public async Task<ScheduleHoliday> CreateHolidayAsync(ScheduleHoliday scheduleHoliday)
        {
            return await _scheduleHolidayRepository.InsertAsync(scheduleHoliday);
        }

        public async Task<ScheduleHoliday> UpdateHolidayAsync(ScheduleHoliday scheduleHoliday)
        {
            return await _scheduleHolidayRepository.UpdateAsync(scheduleHoliday);
        }

        public async Task<ScheduleHoliday> DeleteHolidayAsync(ScheduleHoliday scheduleHoliday)
        {
            try
            {
                await _scheduleHolidayRepository.DeleteAsync(scheduleHoliday);
                scheduleHoliday.IsDeleted = true;
                return scheduleHoliday;
            }
            catch (Exception e)
            {
                scheduleHoliday.IsDeleted = false;
                return scheduleHoliday;
            }
        }

        public Task<List<ScheduleHoliday>> GetHolidayAsync()
        {
            return _scheduleHolidayRepository.GetListAsync();
        }

    }
}
